using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using Destra.Tools.DACGenerator.SqlSuggestor;

namespace Destra.Tools.DACGenerator
{
    public partial class SqlSuggestorDialog : Form
    {
        private string m_connectionString;

        public SqlSuggestorDialog(string connectionString)
        {
            InitializeComponent();

            this.Load += new EventHandler(SqlSuggestorDialog_Load);

            m_connectionString = connectionString;
        }

        private string GetServer()
        {
            string server = null;

            Regex regex = new Regex(@"Data Source=(?<server>[^;]*)",
                RegexOptions.IgnoreCase);
            Match match = regex.Match(m_connectionString);
            if (match.Success == true)
            {
                server = match.Groups["server"].Value;
            }

            return server;
        }

        void SqlSuggestorDialog_Load(object sender, EventArgs e)
        {
            SqlSuggestorSettings settings = new SqlSuggestorSettings();

            sqlSuggestor.Server = GetServer();
            sqlSuggestor.Database = settings.Database;
            sqlSuggestor.Object = settings.Object;

            sqlHierarchySuggestor1.Server = GetServer();
            sqlHierarchySuggestor1.Database = settings.Database;
            sqlHierarchySuggestor1.Object = settings.Object;
        }

        private string _sql = "";
        public string SQL
        {
            get
            {
                return _sql;
            }
        }

        private void useSqlButton_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex.Equals(0))
            {
                if (sqlSuggestor.SQL.Length == 0)
                {
                    MessageBox.Show(this,
                        "No SQL has been suggested",
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);

                    this.DialogResult = DialogResult.None;
                }
                else
                {
                    SqlSuggestorSettings settings = new SqlSuggestorSettings();

                    settings.Database = sqlSuggestor.Database;
                    settings.Object = sqlSuggestor.Object;

                    settings.Save();

                    _sql = sqlSuggestor.SQL;
                }
            }

            if (tabControl1.SelectedIndex.Equals(1))
            {
                if (sqlHierarchySuggestor1.SQL.Length == 0)
                {
                    MessageBox.Show(this,
                        "No SQL has been suggested",
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);

                    this.DialogResult = DialogResult.None;
                }
                else
                {
                    SqlSuggestorSettings settings = new SqlSuggestorSettings();

                    settings.Database = sqlHierarchySuggestor1.Database;
                    settings.Object = sqlHierarchySuggestor1.Object;

                    settings.Save();

                    _sql = sqlHierarchySuggestor1.SQL;

                }
            }
        }

    }
}